package org.wu.reggie.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wu.reggie.common.R;
import org.wu.reggie.entity.User;
import org.wu.reggie.service.UserService;
import org.wu.reggie.utils.ValidateCodeUtils;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /*@PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){

        // 1 获取手机号
        String phone = user.getPhone();

        // 2 随机生成四位验证码
        String code = ValidateCodeUtils.generateValidateCode(4).toString();

        // 3 调用短信平台工具去发送短信

        log.info("========本次验证码为：==========" + code);
        // 4 把验证码存储到session中

        redisTemplate.opsForValue().set(phone,code,1, TimeUnit.MINUTES);


        session.setAttribute(phone,code);// 不好控制时间 后期可以使用redis存储
        return R.success("发送短信成功！");
    }*/
    /*    @PostMapping("/login")
        public R<User> login(@RequestBody User userParam, HttpSession session) {

            // 1 取出参数
            String phone = userParam.getPhone();//获取手机号
            System.out.println("phone = " + phone);
            String code = userParam.getCode(); //用户输入的验证码
            System.out.println("code = " + code);
            String codeInSession = (String) session.getAttribute(phone);
            System.out.println("codeInSession = " + codeInSession);
            // 2 把参数交给Service层 返回当前登录者对象
            User user = userService.login(phone, code, codeInSession);


            if (user != null) {
                session.setAttribute("user", user.getId());
            } else {
                return R.error("验证码错误");
            }

            return R.success(user);
        }*/

    @PostMapping("/login")
    public R<User> login(@RequestBody User userParam,HttpSession session) {

        // 1 取出参数
        String phone = userParam.getPhone();//获取手机号
        System.out.println("phone = " + phone);
        String code = userParam.getCode(); //用户输入的验证码
        System.out.println("code = " + code);
        String redisCode  = (String) redisTemplate.opsForValue().get(phone);
        System.out.println("redisCode = " + redisCode);
        // 2 把参数交给Service层 返回当前登录者对象
        User user = userService.login(phone, code, redisCode);

        if (user != null) {
            redisTemplate.delete(phone);
            session.setAttribute("user",user.getId());
//            redisTemplate.opsForValue().set("user",user.getPhone(),5,TimeUnit.MINUTES);
        } else {
            return R.error("验证码错误");
        }

        return R.success(user);
    }

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user) {

        // 1 获取手机号
        String phone = user.getPhone();

        // 2 随机生成四位验证码
        String code = ValidateCodeUtils.generateValidateCode(4).toString();

        // 3 调用短信平台工具去发送短信

        log.info("========本次验证码为：==========" + code);
        // 4 把验证码存储到session中

        redisTemplate.opsForValue().set(phone, code, 1, TimeUnit.MINUTES);

        return R.success("发送短信成功！");
    }


}
